package objects;


import containers.Canvas;
import containers.Simulation;
import java.awt.Color;
import java.awt.Graphics2D;


/**
 * Bar is essentially a rigid spring.
 * It is drawn as a green line
 * 
 * @author rwb10 & sab60
 */
public class Bar extends Spring {
    /**
     * Dampening constant b for Bars.
     * If too high, the simulation breaks.
     */
    private static double ourBarDampenConstant = 0.005;
    /**
     * Spring constant k for Bars.
     * If too high, the simulation breaks.
     */
    private static double ourBarSpringConstant = 10;

    /**
     * 
     * @param start the mass at the start of the Bar
     * @param end the mass at the end of the Bar
     */
    public Bar (Mass start, Mass end) {
        super(start, end, 0, ourBarSpringConstant);
        // set rest length to the original distance between the two masses.
        setMyLength(Force.distanceBetween(start.getCenter(), end.getCenter()));
    }

    @Override
    public void paint (Graphics2D pen) {
        Canvas.paintLine(pen, getMyStart().getCenter(), getMyEnd().getCenter(),
                Color.GREEN);
    }

    @Override
    public void update (Simulation canvas, double dt) {

        // startForce is the force to be applied to myStart
        Force startForce = dampeningForce(getMyStart().getVelocity(), angle());
        getMyStart().applyForce(startForce);

        // endForce is the force to be applied to myEnd
        Force endForce = dampeningForce(getMyEnd().getVelocity(), angle() + 180);
        getMyStart().applyForce(endForce);

        super.update(canvas, dt);
    }

    private Force dampeningForce(Force velocity, double angle) {
        Force df = new Force(velocity);
        df.scale(ourBarDampenConstant);
        df.setDirection(angle);
        return df;
    }
}
